/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.deram.ctin.dependencies.serv.utils;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.deram.ctin.common.bean.PageResult;
import com.deram.ctin.dependencies.serv.bean.BaseFilter;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author zhangcm
 * @version 1.0, 2017/9/15
 */
public class MybatisUtils {

    private MybatisUtils(){}

    public static Pagination createPagination(Integer pageNo, Integer pageSize){
        return new Pagination(pageNo, pageSize);
    }

    public static <T> PageResult<T> createPageResult(List<T> list, Pagination pagination){
        PageResult<T> pageResult = new PageResult<T>();
        pageResult.setList(list);
        pageResult.setTotal(pagination.getTotal());
        pageResult.setPageNo(pagination.getCurrent());
        pageResult.setPageSize(pagination.getSize());
        return pageResult;
    }

    public static <T> EntityWrapper<T> createEntityWrapper(T entity, BaseFilter baseFilter){
        EntityWrapper<T> ew = new EntityWrapper<>(entity);
        if(StringUtils.isNotEmpty(baseFilter.getSelectBy())){
            ew.setSqlSelect(baseFilter.getSelectBy());
        }
        if(StringUtils.isNotEmpty(baseFilter.getOrderBy())){
            ew.orderBy(baseFilter.getOrderBy());
        }
        return ew;
    }

    public static <T> void fillEntityWrapper(EntityWrapper<T> ew, BaseFilter baseFilter){
        if(null != baseFilter.getOrderBy()){
            ew.orderBy(baseFilter.getOrderBy());
        }
    }
}
